package com.kungfuhacking.wristbandpro.my.presenter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.mqtt.MqttConstant;
import com.kungfuhacking.wristbandpro.my.activity.OldDetailActivity;
import com.kungfuhacking.wristbandpro.my.bean.OldDetail;
import com.kungfuhacking.wristbandpro.utils.Constant;

import java.util.List;

/**
 * Created by Aaron on 17/5/25.
 */

public class AttentionAdapter extends RecyclerView.Adapter<AttentionAdapter.MyViewHolder> implements View.OnClickListener {

    private final String TAG = getClass().getSimpleName();
    private Context context;
    private static final int IS_HEADER = 2;
    private static final int IS_BOTTOM = 3;
    private static final int IS_NORMAL = 1;
    private OldDetail oldDetail;
    private final SharedPreferences sp;
    private List<OldDetail.KeepEntity> keep;
    private OldDetailActivity oldDetailActivity;
    //    private OldDetail.DataEntity oldDetailData;

    public AttentionAdapter(Context ctx, OldDetail oldDetail) {
        this.context = ctx;
        oldDetailActivity = (OldDetailActivity) ctx;
        this.oldDetail = oldDetail;
        sp = context.getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);
    }

//    public OldDetail.DataEntity getOldDetailData() {
//        return oldDetailData;
//    }
//
//    public void setOldDetailData(OldDetail.DataEntity oldDetailData) {
//        this.oldDetailData = oldDetailData;
//    }


    public void setOldDetail(OldDetail oldDetail) {
        this.oldDetail = oldDetail;
    }

    @Override
    public AttentionAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {

            case IS_HEADER:
                return new AttentionAdapter.MyViewHolder(
                        LayoutInflater.from(context).inflate(R.layout.head_attention_view, parent,
                                false), viewType);
            case IS_NORMAL:
                return new AttentionAdapter.MyViewHolder(
                        LayoutInflater.from(context).inflate(R.layout.item_attention, parent,
                                false), viewType);
            case IS_BOTTOM:
                return new AttentionAdapter.MyViewHolder(
                        LayoutInflater.from(context).inflate(R.layout.bottom_attention_view, parent,
                                false), viewType);
        }

        return null;

    }

    @Override
    public void onBindViewHolder(AttentionAdapter.MyViewHolder viewHolder, int position) {

        if (oldDetail == null)
            return;

        switch (getItemViewType(position)) {

            case IS_HEADER://recycleview 的 头
                setHeadInfo(viewHolder);
                break;

            case IS_NORMAL: //正常的item

                --position;
                keep = oldDetail.getKeep();
                if (keep == null)
                    return;

                baseInfo(viewHolder, position);
                //显示的图标
                switch (oldDetail.getMylevel()) {

                    case Constant.POWER_ADMIN://我是管理员
                        //管理员   可以对管理员降级(不包括自己) 可以删除自己,也可以删除别的管理员  可以对 亲人 升级和 降级   可以对朋友删除和升级
                        switch (keep.get(position).getLevel()) {
                            //对方的权限
                            case Constant.POWER_ADMIN://管理员等级
                                adminPermission(viewHolder, position);
                                break;

                            case Constant.POWER_PARENT://亲人等级
                                familyPermission(viewHolder);
                                break;

                            case Constant.POWER_FRIEND://朋友等级
                                friendPermission(viewHolder);
                                break;

                            case Constant.POWER_TEACHER://老师等级
                                nopePermission(viewHolder);
                                break;

                            default:
                                nopePermission(viewHolder);
                                break;

                        }

                        break;

                    case Constant.POWER_PARENT://我是亲人
                        mFamilyPermission(viewHolder, View.INVISIBLE, sp.getString(Constant.USER_ID, "").equals(keep.get(position).getId())
                                ? View.VISIBLE : View.INVISIBLE);
                        break;

                    case Constant.POWER_FRIEND://我是朋友等级
                        mFriendPermission(viewHolder, position);
                        break;

                    case Constant.POWER_TEACHER://我是老师等级
                        nopePermission(viewHolder);
                        break;

                    case Constant.POWER_DEL://我是 被删除用户
                        nopePermission(viewHolder);
                        break;
                    default:
                        break;
                }

                //是自己, 可以删除自己
                if ((sp.getString(Constant.USER_ID, "").equals(keep.get(position).getId())))
                    viewHolder.tvSlideDelete.setVisibility(View.VISIBLE);

                final int currentPosition = position;

                //升级
                viewHolder.tvTopArrow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        upPermission(currentPosition);
                    }
                });

                //降级
                viewHolder.tvBottomArrow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        degreeLevel(currentPosition);
                    }
                });

                //删除
                viewHolder.tvSlideDelete.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        //判断当前点击删除的按钮是否是自己, 如果是自己删除自己则调用 delChild, 否则调用 updatelevel接口
                        if (sp.getString(Constant.USER_ID, "").equals(keep.get(currentPosition).getId()))
                            deleteSelf();
                        else
                            //点击的是删除别人
                            deleteOther(currentPosition);
                    }
                });
                break;
        }

    }

    private void upPermission(int currentPosition) {
        switch (oldDetail.getKeep().get(currentPosition).getLevel()) {
            case Constant.POWER_PARENT://如果是亲人  升级升为管理员
                oldDetailActivity.levelAdjust(Constant.POWER_ADMIN, keep.get(currentPosition).getPhoneNum());
                break;

            case Constant.POWER_FRIEND: //如果是朋友等级, 升级为亲人
                oldDetailActivity.levelAdjust(Constant.POWER_PARENT, keep.get(currentPosition).getPhoneNum());
                break;
            default:
                Toast.makeText(context, "无法升级", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void mFriendPermission(MyViewHolder viewHolder, int position) {
        //朋友等级  可以删除自己
        viewHolder.tvSlideDelete.setVisibility(
                sp.getString(Constant.USER_ID, "").equals(keep.get(position).getId())
                        ? View.VISIBLE : View.INVISIBLE);

        viewHolder.tvTopArrow.setVisibility(View.INVISIBLE);
        viewHolder.tvBottomArrow.setVisibility(View.INVISIBLE);
    }

    private void mFamilyPermission(MyViewHolder viewHolder, int invisible, int visibility) {
        //亲人 没权限可以操作 但是可以删除自己
        viewHolder.tvTopArrow.setVisibility(invisible);
        viewHolder.tvBottomArrow.setVisibility(invisible);
        viewHolder.tvSlideDelete.setVisibility(
                visibility);
    }

    private void nopePermission(MyViewHolder viewHolder) {
        viewHolder.tvTopArrow.setVisibility(View.INVISIBLE);
        viewHolder.tvSlideDelete.setVisibility(View.INVISIBLE);
        viewHolder.tvBottomArrow.setVisibility(View.INVISIBLE);
    }

    private void friendPermission(MyViewHolder viewHolder) {
        //可删 可升  不可降级
        viewHolder.tvTopArrow.setVisibility(View.VISIBLE);
        viewHolder.tvSlideDelete.setVisibility(View.VISIBLE);
        viewHolder.tvBottomArrow.setVisibility(View.INVISIBLE);
    }

    private void familyPermission(MyViewHolder viewHolder) {
        //可升 可降, 可删
        mFamilyPermission(viewHolder, View.VISIBLE, View.VISIBLE);
    }

    private void adminPermission(MyViewHolder viewHolder, int position) {
        //无法升职
        viewHolder.tvTopArrow.setVisibility(View.INVISIBLE);
        //可以降职和删除
        viewHolder.tvSlideDelete.setVisibility(View.VISIBLE);
//                        viewHolder.iv_degrade.setVisibility(View.VISIBLE);
        //如果是自己 则不能降自己
        if (sp.getString(MqttConstant.USERID, "").equals(keep.get(position).getPhoneNum())) {
            viewHolder.tvBottomArrow.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.tvBottomArrow.setVisibility(View.VISIBLE);
        }
    }

    private void baseInfo(MyViewHolder viewHolder, int position) {
        //头像
        viewHolder.sdvHeadPic.setImageURI(Uri.parse(
                TextUtils.isEmpty(keep.get(position).getImgUrl()) ? "" : keep.get(position).getImgUrl()));
        //昵称和手机号
        viewHolder.tvNameTel.setText(keep.get(position).getName() + " (" + keep.get(position).getPhoneNum() + ")");
        //等级
        //设置权限数据
        switch (oldDetail.getKeep().get(position).getLevel()) {

            case Constant.POWER_ADMIN:
                viewHolder.tvGrade.setText("管理员等级");
                break;

            case Constant.POWER_PARENT:
                viewHolder.tvGrade.setText("亲人等级");
                break;

            case Constant.POWER_FRIEND:
                viewHolder.tvGrade.setText("朋友等级");
                break;

            case Constant.POWER_TEACHER:
                viewHolder.tvGrade.setText("老师等级");
                break;

            default:
                viewHolder.tvGrade.setText("");
                break;

        }
    }

    /**
     * 设置头部信息
     */
    private void setHeadInfo(MyViewHolder viewHolder) {
        OldDetail.DataEntity data = oldDetail.getData();

        if (data == null)
            return;

        viewHolder.sdvHeadPic.setImageURI(Uri.parse(
                TextUtils.isEmpty(data.getPicUrl()) ? "" : data.getPicUrl()));
        viewHolder.tv_name.setText(data.getChildName());
        viewHolder.tv_sex.setText(data.getSex());
        viewHolder.tv_birthday.setText(data.getBirthDay());
//        viewHolder.tv_tel.setText(data.getPhoneNum());

//        viewHolder.rl_family.setOnClickListener(this);
        viewHolder.sdvHeadPic.setOnClickListener(this);
        viewHolder.rl_name.setOnClickListener(this);
        viewHolder.rl_sex.setOnClickListener(this);
        viewHolder.rl_birthday.setOnClickListener(this);
    }

    /**
     * 降级
     */
    private void degreeLevel(int currentPosition) {
        switch (keep.get(currentPosition).getLevel()) {

            case Constant.POWER_ADMIN: //如果是管理员 , 降为 亲人
                oldDetailActivity.levelAdjust(Constant.POWER_PARENT, keep.get(currentPosition).getPhoneNum());
                break;
            case Constant.POWER_PARENT: //如果是亲人, 降为朋友
                oldDetailActivity.levelAdjust(Constant.POWER_FRIEND, keep.get(currentPosition).getPhoneNum());
                break;
        }
    }

    /**
     * 删除自己
     */
    private void deleteSelf() {
        //点击的是删除自己
        Log.d(TAG, "删除自己");

        new AlertDialog.Builder(context).setMessage("确定删除该老人").setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
                oldDetailActivity.delArea();

            }
        }).show();
    }

    /**
     * 删除别人
     */
    private void deleteOther(final int currentPosition) {
        //点击的是删除别人
        new AlertDialog.Builder(context).setMessage("确定删除用户 " + keep.get(currentPosition).getName()).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                oldDetailActivity.levelAdjust(Constant.POWER_DEL, keep.get(currentPosition).getPhoneNum());

            }
        }).show();
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0)
            return IS_HEADER;
        else if (position == oldDetail.getKeep().size() + 1)
            return IS_BOTTOM;
        else
            return IS_NORMAL;
    }

    @Override
    public int getItemCount() {

        if (oldDetail != null && oldDetail.getKeep() != null)
            return oldDetail.getKeep().size() + 2;
        else
            return 0;

    }

    public void setUpdatedInfo(String url, String birthday, String name, String sex) {
        oldDetail.getData().setPicUrl(url);
        oldDetail.getData().setBirthDay(birthday);
        oldDetail.getData().setChildName(name);
        oldDetail.getData().setSex(sex);
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

//            case R.id.rl_family://家庭信息
//                oldDetailActivity.familyInfo();
//                break;

            case R.id.sdvOldHeadView://修改头像
                oldDetailActivity.modifyImg();
                break;

            case R.id.rl_name://修改老人昵称
                oldDetailActivity.modifyName();
                break;

            case R.id.rl_sex://修改老人性别
                oldDetailActivity.modifySex();
                break;

            case R.id.rl_birthday://修改老人生日
                oldDetailActivity.modifyBirthday();
                break;

        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_name;
        TextView tv_sex;
        TextView tv_birthday;
        //        TextView tv_tel;
        RelativeLayout rl_name;
        RelativeLayout rl_sex;
        RelativeLayout rl_birthday;
//        RelativeLayout rl_family;

        SimpleDraweeView sdvHeadPic;
        TextView tvGrade;
        TextView tvNameTel;
        TextView tvTopArrow;
        TextView tvBottomArrow;
        TextView tvSlideDelete;

        MyViewHolder(View view, int viewType) {
            super(view);

            switch (viewType) {

                case IS_HEADER:

                    sdvHeadPic = (SimpleDraweeView) view.findViewById(R.id.sdvOldHeadView);

                    tv_name = (TextView) view.findViewById(R.id.tv_name);
                    tv_sex = (TextView) view.findViewById(R.id.tv_sex);
                    tv_birthday = (TextView) view.findViewById(R.id.tv_birthday);
//                    tv_tel = (TextView) view.findViewById(R.id.tv_tel);
                    rl_name = (RelativeLayout) view.findViewById(R.id.rl_name);
                    rl_sex = (RelativeLayout) view.findViewById(R.id.rl_sex);
                    rl_birthday = (RelativeLayout) view.findViewById(R.id.rl_birthday);
//                    rl_family = (RelativeLayout) view.findViewById(rl_family);

                    break;

                case IS_NORMAL:

                    sdvHeadPic = (SimpleDraweeView) view.findViewById(R.id.sdvHeadPic);
                    tvGrade = (TextView) view.findViewById(R.id.tvGrade);
                    tvNameTel = (TextView) view.findViewById(R.id.tvNameTel);
                    tvTopArrow = (TextView) view.findViewById(R.id.tvTopArrow);
                    tvBottomArrow = (TextView) view.findViewById(R.id.tvBottomArrow);
                    tvSlideDelete = (TextView) view.findViewById(R.id.tvSlideDelete);

                    break;

            }

        }
    }
}
